import { TypeOrmModuleOptions } from '@nestjs/typeorm';

import { User } from '@src/user/entity/user.entity';
import { Role } from '@src/role/entity/role.entity';
import { Permission } from '@src/permission/entity/permission.entity';

import { ConfigurationService } from './configuration.service';

export const databaseConfigFactory = (
  config: ConfigurationService,
): TypeOrmModuleOptions => {
  return {
    type: 'mysql',
    connectorPackage: 'mysql2',
    host: config.app.dbHost,
    port: config.app.dbPort,
    username: config.app.dbUser,
    password: config.app.dbPwd,
    database: config.app.dbName,
    entities: [User, Role, Permission],
    synchronize: config.app.nodeEnv !== 'prod',
    logging: config.app.nodeEnv !== 'prod',
    poolSize: 10,
    // mysql2 专用扩展
    extra: {
      authPlugins: ['sha256_password'], // 若用 8.x
    },
  };
};
